import axios from "axios";
import { ElMessage } from "element-plus";
import "element-plus/theme-chalk/el-message.css";
import { useUserStore } from "@/stores/user";
import { useRouter } from "vue-router";
const router=useRouter()
const request = axios.create({
  baseURL: "http://pcapi-xiaotuxian-front-devtest.itheima.net",
  timeout: 5000,
});
request.interceptors.request.use(
  (config) => {
    const userstore = useUserStore();
    const token = userstore.userInfo.token;
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (e) => Promise.reject(e)
);

request.interceptors.response.use(
  (res) => {
    return res;
  },
  (e) => {
    //统一的错误提升
    ElMessage({
      type: "warning",
      message: e.response.data.message || "请求失败",
    });
/*     if (e.response.status === 401) {
      const userstore = useUserStore();
      userstore.logout();
      router.push("/login");
    } */
    return Promise.reject(e);
  }
);
export { request };
